package main

import (
	"flag"
	"fmt"
	"io"
	"net"
	"os"
)

type Client struct {
	ServerIp   string
	ServerPort int
	Name       string
	//conn       net.Listener
	conn net.Conn

	flag int
}

func NewClient(serverIp string, serverPort int) *Client {
	// 创建客户端对象  init
	client := &Client{
		ServerIp:   serverIp,
		ServerPort: serverPort,
		flag:       88,
	}
	// 链接Server
	conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", serverIp, serverPort))
	//conn, err := net.Listen("tcp", fmt.Sprintf("%s:%s", "本机IP", 8080))
	if err != nil {
		fmt.Println("net.Dail error:", err)
		return nil
	}
	client.conn = conn
	//返回对象
	return client

}
func (client *Client) menu() bool {
	var flag int
	fmt.Println("1.公共聊天")
	fmt.Println("2.私有聊天")
	fmt.Println("3.更新用户名")
	fmt.Println("0.退出")

	fmt.Scanln(&flag)

	if flag >= 0 && flag <= 3 {
		client.flag = flag
		return true
	} else {
		fmt.Println("请输入符合范围内的数字")
		return false
	}
}

func (client *Client) Run() {
	for client.flag != 0 {
		for client.menu() != true {

		}
		switch client.flag {

		case 1:
			fmt.Println("Running 公聊模式")
			client.PublicChat()
			break
		case 2:
			fmt.Println("私聊模式选择")
			client.PrivateChat()
			break
		case 3:
			fmt.Println("更改用户名选择")
			client.UpdateName()
			break
		}
	}
}

func (client *Client) UpdateName() bool {
	fmt.Println("please input a name")
	fmt.Scanln(&client.Name)

	sendMsg := "rename|" + client.Name + "\n"
	_, err := client.conn.Write([]byte(sendMsg))
	if err != nil {
		fmt.Println("error:", err)
		return false
	}
	return true
}

func (client *Client) DealResponse() {
	// 永久阻止监听
	io.Copy(os.Stdout, client.conn)
}

//查询在线用户
func (client *Client) SelectUsers() {
	sendMsg := "who\n"
	_, err := client.conn.Write([]byte(sendMsg))
	if err != nil {
		fmt.Println("error", err)
		return
	}
}

//私聊模式
func (client *Client) PrivateChat() {
	var remoteName string
	var chatMsg string
	client.SelectUsers()
	fmt.Println("!!!请输入聊天对象user，exit退出")
	fmt.Scanln(&chatMsg)

	for remoteName != "exit" {
		fmt.Println("please enter")
		fmt.Scanln(&chatMsg)

		for chatMsg != "exit" {
			if len(chatMsg) != 0 {
				sendMsg := "to|" + remoteName + "|" + chatMsg + "\n\n"
				_, err := client.conn.Write([]byte(sendMsg))
				if err != nil {
					fmt.Println("conn write error: ", err)
					break
				}
			}

			chatMsg = ""
			fmt.Println("please enter")
			fmt.Scanln(&chatMsg)
		}
		client.SelectUsers()
		fmt.Println("!!!请输入聊天对象user，exit退出")
		fmt.Scanln(&remoteName)

	}
}
func (client *Client) PublicChat() {
	var chatMsg string

	fmt.Println("请输入聊 xxx")
	fmt.Scanln(&chatMsg)

	for chatMsg != "exit" {
		if len(chatMsg) != 0 {
			sendMsg := chatMsg + "\n"
			_, err := client.conn.Write([]byte(sendMsg))
			if err != nil {
				fmt.Println("conn write error: ", err)
				break
			}
		}
	}

	chatMsg = ""
	fmt.Println("请输入聊 xxx")
	fmt.Println(&chatMsg)
}

var serverIp string
var serverPort int

//./client -ip 127.0.0.1 -port 8080

func init() {
	flag.StringVar(&serverIp, "serverip", "127.0.0.1", "IP address to connect to")
	flag.IntVar(&serverPort, "serverPort", 8080, "设置服务器端口 默认为8080")

	//net.Listen("tcp", fmt.Sprintf("%s:%s", "本机IP", 8080))

}

func main() {
	// 命令行解析
	//flag.Parse()
	client := NewClient(serverIp, serverPort)

	if client == nil {
		fmt.Println("链接服务器失败！！！！")
		return
	}

	// 单独开启一个goroutine 去处理server的回执消息
	go client.DealResponse()
	fmt.Println("client链接成功")

	//启动客户端的业务
	client.Run()
}
